<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Option;
use Illuminate\Http\Request;
class OptionController extends Controller{

    public function index(Request $request,$symbol){
        //$res = get_settle_option_data('HIHSI10','2016-10-24 17:33:25');
        //var_dump($res);
        $user = $request->user();
        $option = Option::select('id','name', 'code','times')->where('code','=',$symbol)->first();
        if(!$option){
            return redirect('/');
        }
        //get_option_data('PMAU');
        $times = $option->times;
        
        $times = explode(',',$times);
        $newtimes = [];
        foreach($times as $k=>$time){
            list($start,$end) = explode('-',$time);
            if($start>=$end){
                $periodone = $start.'-24:00';
                $periodtwo = "00:00-".$end;
                //unset($times[$k]);
                $newtimes[] = $periodone;
                $newtimes[] = $periodtwo;
            }else{
                $newtimes[] = $time;
            }
        }
        //$times =implode(',',$times);
        $newtimes = json_encode($newtimes);
        $optionData= get_option_data($symbol);

        $otherOptions = Option::where('code','!=',$symbol)->get();

        return view('home.option.index',compact('user','option','newtimes','optionData','otherOptions'));
    }

    /**
     * [rules 交易规则]
     * @param  Request $request [description]
     * @param  [type]  $symbol  [description]
     * @return [type]           [description]
     */
    public function rules(Request $request,$symbol){
         $option = Option::where('code','=',$symbol)->first();
         return view('home.option.rules',compact('option'));
    }


}